package fr.pud.client.controller.query;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComboBox;
import javax.swing.JTextField;

import fr.pud.client.model.PUDStation;
import fr.pud.client.model.PUDStationDatabase;

/**
 * Apr 17, 2012 - Projet_Ose.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         SuggestDataAccordingToInput.java
 *         This class select and display on a JComboBox data according to the
 *         input search.
 */
public class SuggestionsForJComboBox implements KeyListener, FocusListener {
    /**
     * The JComboBox where the data are displayed.
     */
    private JComboBox  comboBox;
    /**
     * The JtextField where the search is done.
     */
    private JTextField textField;
    /**
     * The string typed by the user.
     */
    private String     lastInput = " ";

    /**
     * The default constructor.
     */
    public SuggestionsForJComboBox(JTextField aTextField, JComboBox aComboBox) {
        this.textField = aTextField;
        this.comboBox = aComboBox;
    }

    /*
     * (non-Javadoc)
     * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
     */
    @Override
    public void focusGained(FocusEvent aE) {
        this.suggestData();
    }

    /*
     * (non-Javadoc)
     * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
     */
    @Override
    public void focusLost(FocusEvent aE) {
        this.suggestData();
    }

    /**
     * @return the comboBox
     */
    public JComboBox getComboBox() {
        return this.comboBox;
    }

    /**
     * @return the textField
     */
    public JTextField getTextField() {
        return this.textField;
    }

    /*
     * (non-Javadoc)
     * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
     */
    @Override
    public void keyPressed(KeyEvent aArg0) {
        this.suggestData();
    }

    /*
     * (non-Javadoc)
     * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
     */
    @Override
    public void keyReleased(KeyEvent aArg0) {
        this.suggestData();
    }

    /*
     * (non-Javadoc)
     * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
     */
    @Override
    public void keyTyped(KeyEvent aArg0) {
        this.suggestData();
    }

    /**
     * If an address contains the input of the user, thus one is added to the
     * JComboBox;
     */
    private void suggestData() {
        String input = this.textField.getText();
        this.textField.setBackground(Color.ORANGE);
        this.comboBox.removeAllItems();
        for (PUDStation pud : PUDStationDatabase.getInstance().getStations()) {
            if (pud.toString().toLowerCase().contains(input.toLowerCase())) {
                this.comboBox.addItem(pud);
                this.textField.setBackground(Color.GREEN);
            }
        }
    }
}
